package com.archie.xunji.guanzhu.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.archie.xunji.R;
import com.archie.xunji.guanzhu.entitys.Attention;
import com.lidroid.xutils.BitmapUtils;
import com.meg7.widget.CircleImageView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by xqx on 2015/11/10.
 */
public class AttentionAdapter extends BaseAdapter{
    private List<Attention> list;
    private Context context;
    private BitmapUtils bitmapUtils;

    private View.OnClickListener onAttitudeListener;
    private View.OnClickListener onFavouriteListener;
    private View.OnClickListener onMusicPlay;
    public AttentionAdapter(List<Attention> list, Context context) {
        this.list = list;
        this.context = context;
        bitmapUtils = new BitmapUtils(context);
    }

    /**
     * 监听声音播放按钮响应s
     * @param onMusicPlay
     */
    public void setOnMusicPlay(View.OnClickListener onMusicPlay){
        this.onMusicPlay = onMusicPlay;
    }
    /**
     * 监听点赞按钮点击的事件
     * @param onAttitudeListener
     */
    public void setOnAttentionChangeListener(View.OnClickListener onAttitudeListener){
        this.onAttitudeListener = onAttitudeListener;
    }

    /**
     * 监听收藏的按钮点击事件
     * @param onFavouriteListener
     */
    public void setOnFavouriteChangeListener(View.OnClickListener onFavouriteListener){
        this.onFavouriteListener = onFavouriteListener;
    }


    @Override
    public int getCount() {
        int ret = 0;
        if (list != null) {
            ret = list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.item_attention_detail,parent,false);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.list_detail_description = (TextView) view.findViewById(R.id.list_detail_description);
            holder.list_detail_username = (TextView) view.findViewById(R.id.list_detail_username);
            holder.list_detail_playcount = (TextView) view.findViewById(R.id.list_detail_playcount);
            holder.list_detail_headurl = (ImageView) view.findViewById(R.id.list_detail_headurl);
            holder.list_detail_music_play_img = (ImageView) view.findViewById(R.id.list_detail_music_play_img);
            holder.list_detail_userimg = (CircleImageView) view.findViewById(R.id.list_detail_userimg);
            holder.list_detail_commentcount = (TextView) view.findViewById(R.id.list_detail_commentcount);
            holder.list_detail_attitude = (TextView) view.findViewById(R.id.list_detail_attitude);
            holder.list_detail_labels = (TextView) view.findViewById(R.id.list_detail_labels);
            holder.list_detail_createtime = (TextView) view.findViewById(R.id.list_detail_createtime);
            holder.list_detail_location = (TextView) view.findViewById(R.id.list_detail_location);

            //设置按钮监听事件
            //赞
            holder.btn_list_detail_attitude = (ImageButton) view.findViewById(R.id.btn_list_detail_attitude);
            holder.btn_list_detail_attitude.setOnClickListener(onAttitudeListener);
            //收藏
            holder.list_detail_favourite = (ImageButton) view.findViewById(R.id.list_detail_favourite);
            holder.list_detail_favourite.setOnClickListener(onFavouriteListener);
            //播放声音
            holder.list_detail_music_play = (TextView) view.findViewById(R.id.list_detail_music_play);
            holder.list_detail_music_play.setOnClickListener(onMusicPlay);
            view.setTag(holder);
        }
        holder.list_detail_description.setText(list.get(position).getDescription());
        holder.list_detail_username.setText(list.get(position).getUsername());
        holder.list_detail_playcount.setText("播放 "+list.get(position).getPlaycount());
        holder.list_detail_commentcount.setText(list.get(position).getCommentcount() + "");
        holder.list_detail_labels.setText(list.get(position).getLabels());
        holder.list_detail_attitude.setText(list.get(position).getAttitudecount() + "");
        holder.list_detail_location.setText(list.get(position).getPlacename() + "");

        //头部头像
        bitmapUtils.display(holder.list_detail_userimg,"http://imagecdn.xunjimap.com/image/"+list.get(position).getHeadurl());
        //点赞
        holder.btn_list_detail_attitude.setTag(position);
        holder.btn_list_detail_attitude.setImageResource(list.get(position).isAttituded() ? R.mipmap.ui_comm_attituded : R.mipmap.ui_comm_attitude);
        //收藏
        holder.list_detail_favourite.setTag(position);
        holder.list_detail_favourite.setImageResource(list.get(position).isFavourited() ? R.mipmap.ui_comm_favourited : R.mipmap.ui_comm_favourite);
        //播放声音
        holder.list_detail_music_play.setTag(position);
        String surplusTime = list.get(position).getSurplusTime();
        int audiolength = Integer.parseInt(surplusTime);
        holder.list_detail_music_play.setText(audiolength / 60 + "'" + audiolength % 60);
        holder.list_detail_music_play_img.setImageResource(list.get(position).isPlaying()?R.mipmap.ui_publish_recordfinished_pause:R.mipmap.ui_publish_recordfinished_play);

        //获得当前时间
        long time = Long.valueOf(list.get(position).getCreatedate());
        SimpleDateFormat createTime = new SimpleDateFormat("MM-dd HH:mm");
        holder.list_detail_createtime.setText(createTime.format(new Date(time)));

        bitmapUtils.display(holder.list_detail_headurl,"http://imagecdn.xunjimap.com/image/"+list.get(position).getImageurl());


        return view;
    }

    public class ViewHolder{
        //详情描述
        private TextView list_detail_description;
        // 用户名
        private TextView list_detail_username;
        //播放数量
        private TextView list_detail_playcount;
        //头部图片
        private ImageView list_detail_headurl;
        //评论
        private TextView list_detail_commentcount;
        //赞
        private TextView list_detail_attitude;
        //赞 按钮
        private ImageButton btn_list_detail_attitude;
        //收藏 按钮
        private ImageButton list_detail_favourite;
        //标签
        private TextView list_detail_labels;
        //创建时间
        private TextView list_detail_createtime;
        //坐标
        private TextView list_detail_location;
        //声音播放
        //时间
        private TextView list_detail_music_play;
        //图片
        private ImageView list_detail_music_play_img;
        //用户头像
        private CircleImageView list_detail_userimg;
    }
}
